/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.component;

import com.funcom.peeler.WorkBench;
import com.funcom.peeler.peelable.PeelCanvasWindow;
import com.jme.renderer.Renderer;
import com.jmex.bui.BComponent;
import org.lwjgl.opengl.GL11;

import java.awt.*;

/**
 * Is the grid on the workspace. Sole responsibility is to draw properly when asked to
 */
public class Grid extends BComponent {

    private BComponent parent;
    private WorkBench.Bui bui;

    /**
     * Constructor for the grid
     * @param parent BComponent to center on, cannot be null
     * @param bui The WorkBench.Bui that contains the component
     */
    public Grid( BComponent parent, WorkBench.Bui bui ) {
        if ( parent == null ) throw new NullPointerException( "Grid must be intialized with a valid parent BComponent" );
        this.parent = parent;
        this.bui = bui;
    }

    /**
     * Changes the component that the grid centers on
     * @param new_parent The new BComponent to center on
     */
    public void setParent( BComponent new_parent ) {
        if ( new_parent == null ) return;
        this.parent = new_parent;
    }

    /**
     * Draw the grid pattern on the workbench with an offset large enough to
     * have it centered on the canvas window's lower left corner
     *
     * @param renderer the renderer to use in drawing
      */
    public void render( Renderer renderer ) {

        if ( !bui.getPeelRoot().getToolBar().gridEnabled() ) return;

        int grid_step = bui.getPeelRoot().getToolBar().getGridStep();

        // Find the proper offset
        PeelCanvasWindow canvas = bui.getWorkSpace().getCanvas();
        final Point offset = new Point( canvas.getComponent().getX() % grid_step, canvas.getComponent().getY() % grid_step );

        int parent_x = bui.getWorkSpace().getComponent().getX();
        int parent_y = bui.getWorkSpace().getComponent().getY();
        int step_x = 0;
        int step_y = 0;

        GL11.glColor3f( 0, 0, 0 );
        GL11.glEnable( GL11.GL_LINE_STIPPLE );
        GL11.glLineWidth( 1 );
        GL11.glPushAttrib( GL11.GL_LINE_BIT );
        GL11.glLineStipple( 2, (short)0xAAAA );
        GL11.glBegin( GL11.GL_LINES );

        while ( step_x < parent.getWidth() && step_y < parent.getHeight() ) {

            GL11.glVertex2f( parent_x + step_x + offset.x, 0 );
            GL11.glVertex2f( parent_x + step_x + offset.x, parent.getHeight() );
            GL11.glVertex2f( 0, parent_y + step_y + offset.y );
            GL11.glVertex2f( parent.getWidth(), parent_y + step_y + offset.y );

            step_x += grid_step;
            step_y += grid_step;

        }
        GL11.glEnd();
    }
}
